package component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigUtils;
import service.UserService;
import service.impl.UserServiceImpl;
import service.impl.UserServiceImplTest2;

/**
 * @author: ZhouBert
 * @date: 2020/9/17
 * @description:	同 TestBeanDefinitionRegistryPostProcessor TestBeanDefinitionRegistryPostProcessorPriorityOrdered
 * 至于：为什么 TestBeanDefinitionRegistryPostProcessor 同样继承了 BeanDefinitionRegistryPostProcessor 但是木有提供 supplier 呢？
 * 因为这里通过 调用 new RootBeanDefinition(beanName, Supplier) 的构造方法传入的！
 * 而且，本身 Supplier 是一个 lambda 接口！
 * 而且，正是在 此处，完成 userService 的 b.d. 注册！
 *
 *
 */
public class MyInstanceSupplierBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		registry.registerBeanDefinition("userService",new RootBeanDefinition(UserService.class, () -> {
			System.out.println("UserDao自定义提供器");
			return new UserServiceImplTest2();
		}));

		//可以覆盖 org.springframework.context.annotation.internalConfigurationBeanNameGenerator ！
		// spring 的拓展性真的牛逼！
		registry.registerBeanDefinition(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, new RootBeanDefinition(UserService.class, () -> {
			System.out.println("修改 "+ AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR);
			return new UserServiceImpl();
		}));
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

	}
}
